package com.redstar.HappyRefresh.controller;

import com.redstar.HappyRefresh.constant.RedisConstant;
import com.redstar.HappyRefresh.pojo.*;
import com.redstar.HappyRefresh.service.TypeService;
import com.redstar.HappyRefresh.service.VideoService;
import com.redstar.HappyRefresh.util.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@Slf4j
@RequestMapping("/luckyjourney/index")
public class IndexController { // 初始化

    @Autowired
    private VideoService videoService;
    @Autowired
    private TypeService typeService;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @GetMapping("/pushVideos")
    public Result pushVideos(HttpServletRequest request) { // 兴趣推送视频
        Long userId = JwtUtils.getUserId(request);
        return Result.succeed().data(videoService.pushVideos(userId));
    }

    @GetMapping("/search")
    public Result searchVideo(@RequestParam(required = false) String searchName, BasePage basePage, HttpServletRequest request) { // 搜索视频
        Long userId = JwtUtils.getUserId(request);
        return Result.succeed().data(videoService.searchVideo(searchName, basePage, userId));
    }

    @GetMapping("/video/type/{typeId}")
    public Result getVideoByTypeId(@PathVariable Long typeId) { // 根据分类随机推送视频
        if (typeId == null) {
            log.info("getVideoByTypeId方法：分类id为null，这让我推送个锤子的视频");
            return Result.succeed().message("分类id为null，这让我推送个锤子的视频");
        }
        return Result.succeed().data(videoService.getVideoByTypeId(typeId));
    }

    @GetMapping("/types")
    public Result getTypes(HttpServletRequest request) { // 查询所有分类
        Long userId = JwtUtils.getUserId(request);
        return Result.succeed().data(typeService.getTypes(userId));
    }

    @PostMapping("/share/{videoId}")
    public Result share(@PathVariable Long videoId, HttpServletRequest request) { // 分享视频
        if (ObjectUtils.isEmpty(videoId)) {
            log.error("share方法：videoId为null，我哪知道你分享了啥视频");
            return Result.fail().message("videoId为null，我哪知道你分享了啥视频");
        }
        String message = videoService.share(videoId, request);
        if ("success".equals(message)) {
            return Result.succeed().message("分享成功");
        } else {
            log.error("share方法：" + message);
            return Result.fail().message(message);
        }
    }

    @GetMapping("/video/{id}")
    public Result getVideoById(@PathVariable Long id, HttpServletRequest request) { // 根据视频id获取视频详情
        Long userId = JwtUtils.getUserId(request);
        if (userId == null) {
            log.info("无法从request中取出userId，当前用户为游客登陆");
        }
        return Result.succeed().data(videoService.getVideoById(id, userId));
    }

    @GetMapping("/video/hot/rank")
    public Result listHotRank() { // 获取热门排行榜
        return Result.succeed().data(videoService.listHotRank());
    }

    @GetMapping("/video/similar")
    public Result pushVideoSimilar(Video video) { // 根据视频标签推送相似视频
        if (!StringUtils.hasLength(video.getLabelNames())) {
            log.error("pushVideoSimilar方法：video中的标签为null");
            return Result.fail().message("video中的标签为null");
        }
        return Result.succeed().data(videoService.pushVideoSimilar(video));
    }

    @GetMapping("/video/hot")
    public Result listHotVideo() { // 推送热门视频
        return Result.succeed().data(videoService.listHotVideo());
    }

    @GetMapping("/video/user")
    public Result listVideoByUserId(@RequestParam(required = false) Long userId, BasePage basePage, HttpServletRequest request) { //  根据用户id获取视频
        // userId为null，说明获取的是当前用户的视频
        if (userId == null) {
            userId = JwtUtils.getUserId(request);
        }
        return Result.succeed().data(videoService.listVideoByUserId(userId, basePage));
    }

    @GetMapping("/search/history")
    public Result searchHistory(HttpServletRequest request) { // 获取用户的搜索记录
        Long userId = JwtUtils.getUserId(request);
        return Result.succeed().data(videoService.searchHistory(userId));
    }

    @DeleteMapping("/search/history")
    public Result deleteSearchHistory(HttpServletRequest request) { // 删除搜索记录，全部删除
        Long userId = JwtUtils.getUserId(request);
        String key = RedisConstant.USER_SEARCH_HISTORY + userId;
        redisTemplate.delete(key);
        return Result.succeed().message("搜索记录已删除");
    }
}
